//
//  ServerFactory.swift
//  Asman
//
//  Created by 李旭峰 on 5/16/19.
//  Copyright © 2019 李旭峰. All rights reserved.
//

import Foundation
import Alamofire
import RxCocoa
import RxSwift
class ServerFactory {
    let dispose = DisposeBag()
    lazy var serverStatus: ServerStatus = .cxRelese_ipv4
    var isDidCheckServer:Bool = true
    let checkServerSignal = PublishSubject<Bool>()
    static let `default` = ServerFactory()
    
    var servers = [String : Serverable]()
    private init() {
    }
    
    func server(identifier: Service) -> Serverable {
        var server: Serverable
        if let ser = servers[identifier.rawValue] {
            server = ser
        }else {
            server = createServer(identifier)
        }
        server.serverStatus = self.serverStatus
        servers.updateValue(server, forKey: identifier.rawValue)
        return server
    }
    
    func createServer(_ identifier: Service) -> Serverable {
        if identifier == Service.userHost {
            return UserHostServer()
        }else if identifier == Service.smsHost {
            return SmsHostServer()
        }else if identifier == Service.host {
            return HostServer()
        }else if identifier == Service.webSocket {
            return WebSocketServer()
        }
        return HostServer()
    }
}
